const { STRING, INTEGER } = require("sequelize");
const sequelize = require("../sequelize")
const role = sequelize.define('roles', {
    rolename: {
        type: STRING,
        allowNull: false,
        unique: true,
        comment: '角色名称'
    },
    rolekey: {
        allowNull: false,
        type: STRING,
        comment: '角色权限的字符串'
    },
    rolesort: {
        type: INTEGER,
        comment: '显示顺序'
    },
    datascope: {
        type: STRING,
        defaultValue: '1',
        comment: '数据范围（1：本部门及以下数据权限 2：本部门数据权限 3：仅本人权限）'
    },
    rolestatus: {
        type: STRING,
        defaultValue: '1',
        comment: '角色状态（1正常 0停用）'
    },
    isdelete: {
        type: STRING,
        defaultValue: '0',
        comment: '删除标志（0代表存在 1代表删除）'
    },
    remark: {
        type: STRING,
        comment: '角色描述'
    },
    createdate: {
        allowNull: true,
        type: STRING(255),
        comment: '创建时间'
    },
    creator: {
        allowNull: true,
        type: STRING,
        comment: '创建者'
    },
    updated: {
        allowNull: true,
        type: STRING,
        comment: '更新时间'
    },
    updator: {
        allowNull: true,
        type: STRING,
        comment: '更新者'
    }
}, { timestamps: false });

const rule = require("./rule")
const rolerule = require("./rolerule")
role.belongsToMany(rule, { through: rolerule });
const user = require("./user")
role.associate = () => {
    role.hasMany(user, { foreignKey: "role_id", targetKey: "id" })
}

module.exports = role